package com.zy.algorithm.leetcode;

import java.util.Arrays;

/**
 * @author ZhaoYi
 * @date 2023/11/13 16:14
 * https://leetcode.cn/leetbook/read/sort-algorithms/evfj1d/
 * 给定一个数组 nums，编写一个函数将所有 0 移动到数组的末尾，同时保持非零元素的相对顺序。
 * 请注意 ，必须在不复制数组的情况下原地对数组进行操作。
 *
 * 输入: nums = [0,1,0,3,12]
 * 输出: [1,3,12,0,0]
 */
public class Zeroes {
    public static void main(String[] args) {
        Zeroes zeroes = new Zeroes();

        //int[] nums = {0, 1, 0, 3, 12};
        //int[] nums = {0, 1, 0, 0, 3, 12};
        int[] nums = {0, 0, 0, 0, 0, 12};

        zeroes.moveZeroes(nums);

        System.out.println("nums = " + Arrays.toString(nums));
    }

    public void moveZeroes(int[] nums) {
        if (nums == null || nums.length < 2) {
            return;
        }

        for (int i = 0; i < (nums.length - 1); i++) {
            int left = nums[i];
            int right = nums[i + 1];
            System.out.println(left + "--" +right);
            if (left == 0 && right != 0) {
                nums[i] = right;
                nums[i + 1] = 0;
            }


        }


    }

}
